package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.RoleDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


//角色

@RestController
@RequestMapping("Role")
public class RoleController extends BaseController {

    @Autowired
    @Qualifier("RoleServiceImplOne")
    private RoleService roleService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("RoleMenuRelServiceImplOne")
    private RoleMenuRelService roleMenuRelService;

    @Autowired
    @Qualifier("MenuServiceImplOne")
    private MenuService menuService;

    @Autowired
    @Qualifier("GovernmentServiceImplOne")
    private GovernmentService governmentService;


    //列表,超级管理员添加的角色列表
    @RequestMapping(value = "ListSuper",method = RequestMethod.POST)
    public MyResult ListSuper(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Role/ListSuper");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<Role> listRole = roleService.getList();
            List<Role> listRoleNew = new ArrayList<>();
            for(Role role:listRole) {
                if(role.getName().equals("系统管理员") || role.getName().equals("政府管理员")){
                    listRoleNew.add(role);
                }
            }

            List<RoleDTO> listRoleDTO = new ArrayList<>();
            for (Role role:listRoleNew) {
                RoleDTO roleDTO = mapper.map(role, RoleDTO.class);
                listRoleDTO.add(roleDTO);
            }

            for(RoleDTO roleDTO: listRoleDTO){
                List<RoleMenuRel> listRoleMenuRel = roleMenuRelService.getListByRoleCode(roleDTO.getRoleCode());
                List<Menu> listMenu = new ArrayList<>();
                for(RoleMenuRel roleMenuRel:listRoleMenuRel){
                    Menu menu = menuService.getByMenuCode(roleMenuRel.getMenuCode());
                    listMenu.add(menu);
                }
                roleDTO.setListMenu(listMenu);
            }


            return success(listRoleDTO);

        }catch (Exception e){
            insertError("/Role/ListSuper",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //列表,政府管理员添加的角色列表
    @RequestMapping(value = "ListGov",method = RequestMethod.POST)
    public MyResult ListGov(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Role/ListGov");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            List<Role> listRole = roleService.getList();
            List<Role> listRoleNew = new ArrayList<>();
            for(Role role:listRole) {
                if(!role.getName().equals("超级管理员") &&
                        !role.getName().equals("系统管理员") &&
                        !role.getName().equals("省民政") &&
                        !role.getName().equals("市民政") &&
                        !role.getName().equals("县区民政") &&
                        !role.getName().equals("街道民政") &&
                        !role.getName().equals("居委会") &&
                        !role.getName().equals("政府管理员")){
                    listRoleNew.add(role);
                }
            }

            List<RoleDTO> listRoleDTO = new ArrayList<>();
            for (Role role:listRoleNew) {
                RoleDTO roleDTO = mapper.map(role, RoleDTO.class);
                listRoleDTO.add(roleDTO);
            }

            for(RoleDTO roleDTO: listRoleDTO){
                List<RoleMenuRel> listRoleMenuRel = roleMenuRelService.getListByRoleCode(roleDTO.getRoleCode());
                List<Menu> listMenu = new ArrayList<>();
                for(RoleMenuRel roleMenuRel:listRoleMenuRel){
                    Menu menu = menuService.getByMenuCode(roleMenuRel.getMenuCode());
                    listMenu.add(menu);
                }
                roleDTO.setListMenu(listMenu);
            }


            return success(listRoleDTO);

        }catch (Exception e){
            insertError("/Role/ListGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //政府管理员添加的角色列表
    @RequestMapping(value = "ListGov1",method = RequestMethod.POST)
    public MyResult ListGov1(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Role/ListGov");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            List<Role> listRole = roleService.getList();
            List<Role> listRoleNew = new ArrayList<>();
            for(Role role:listRole) {
                if(!role.getName().equals("超级管理员") &&
                        !role.getName().equals("系统管理员") &&
                        !role.getName().equals("商家") &&
                        !role.getName().equals("客服") &&
                        !role.getName().equals("政府管理员")){
                    listRoleNew.add(role);
                }
            }

            List<RoleDTO> listRoleDTO = new ArrayList<>();
            for (Role role:listRoleNew) {
                RoleDTO roleDTO = mapper.map(role, RoleDTO.class);
                listRoleDTO.add(roleDTO);
            }

            for(RoleDTO roleDTO: listRoleDTO){
                List<RoleMenuRel> listRoleMenuRel = roleMenuRelService.getListByRoleCode(roleDTO.getRoleCode());
                List<Menu> listMenu = new ArrayList<>();
                for(RoleMenuRel roleMenuRel:listRoleMenuRel){
                    Menu menu = menuService.getByMenuCode(roleMenuRel.getMenuCode());
                    listMenu.add(menu);
                }
                roleDTO.setListMenu(listMenu);
            }


            return success(listRoleDTO);

        }catch (Exception e){
            insertError("/Role/ListGov1",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //政府管理员添加组织账号
    @RequestMapping(value = "ListForGov",method = RequestMethod.POST)
    public MyResult ListForGov(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Role/ListForGov");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            List<Role> listRole = roleService.getList();
            List<Role> listRoleNew = new ArrayList<>();
            for(Role role:listRole) {
                if(role.getName().equals("组织账号")){
                    listRoleNew.add(role);
                }
            }

            return success(listRoleNew);
        }catch (Exception e){
            insertError("/Role/ListForGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //角色列表
    @RequestMapping(value = "ListGovRole",method = RequestMethod.POST)
    public MyResult ListGovRole(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Role/ListGovRole");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            List<Role> listRole = roleService.getList();
            return success(listRole);
        }catch (Exception e){
            insertError("/Role/ListGovRole",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }







    //列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Role/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            PageInfo<RoleDTO> pageInfoRoleDTO = roleService.getPageInfoRoleDTO(pageIndex,pageSize);
            List<RoleDTO> listRoleDTO = pageInfoRoleDTO.getList();
            for(RoleDTO roleDTO : listRoleDTO){
                List<RoleMenuRel> listRoleMenuRel = roleMenuRelService.getListByRoleCode(roleDTO.getRoleCode());
                List<Menu> listMenu = new ArrayList<>();
                for(RoleMenuRel roleMenuRel:listRoleMenuRel){
                    Menu menu = menuService.getByMenuCode(roleMenuRel.getMenuCode());
                    if(menu != null){
                        listMenu.add(menu);
                    }
                }
                roleDTO.setListMenu(listMenu);
            }

            return success(pageInfoRoleDTO);

        }catch (Exception e){
            insertError("/Role/Page",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //添加角色菜单的关系
    void AddRoleMenuRel(String roleCode, JSONArray arr) {
        for (int k = 0; k < arr.size(); k++) {
            JSONObject menu = arr.getJSONObject(k);

            RoleMenuRel roleMenuRel = new RoleMenuRel();
            roleMenuRel.setRoleMenuRelCode(UUID.randomUUID().toString());
            roleMenuRel.setRoleCode(roleCode);
            roleMenuRel.setMenuCode(menu.getString("menuCode"));
            roleMenuRelService.insert(roleMenuRel);

            JSONArray listMenu = menu.getJSONArray("listMenu");
            if (listMenu != null) {
                AddRoleMenuRel(roleCode, listMenu);
            }

        }

    }


    //添加
    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            JSONArray listMenu = jsonObject.getJSONArray("listMenu");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Role/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            Role roleTemp = roleService.getByName(name);
            if(roleTemp != null){
                return failed("角色名称已存在");
            }


            Role role = new Role();
            role.setRoleCode(UUID.randomUUID().toString());
            role.setName(name);
            role.setSort(sort);
            roleService.insert(role);

            if (listMenu != null) {
                AddRoleMenuRel(role.getRoleCode(),listMenu);
            }


            Object object = new Object[]{};
            return success(object);

        }catch (Exception e){
            insertError("/Role/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //编辑
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            JSONArray listMenu = jsonObject.getJSONArray("listMenu");

            String roleCode = jsonObject.getString("roleCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Role/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            Role roleTemp = roleService.getByName(name);
            if(roleTemp != null && !roleCode.equals(roleTemp.getRoleCode())){
                return failed("角色名称已存在");
            }


            Role role = roleService.getByRoleCode(roleCode);
            role.setName(name);
            role.setSort(sort);
            roleService.updateByRoleCode(role);


            List<RoleMenuRel> listRoleMenuRel = roleMenuRelService.getListByRoleCode(roleCode);
            for (RoleMenuRel roleMenuRel : listRoleMenuRel) {
                roleMenuRelService.deleteByRoleMenuRelCode(roleMenuRel.getRoleMenuRelCode());
            }


            if (listMenu != null) {
                AddRoleMenuRel(role.getRoleCode(),listMenu);
            }


            Object object = new Object[]{};
            return success(object);

        }catch (Exception e){
            insertError("/Role/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "GetByRoleCode",method = RequestMethod.POST)
    public MyResult GetByRoleCode(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String roleCode =jsonObject.getString("roleCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Role/GetByRoleCode");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Role role =roleService.getByRoleCode(roleCode);

            JSONObject jsonObject1=new JSONObject();
            jsonObject1.put("roleName",role.getName());

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Role/GetByRoleCode",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }





}
